package com.jt.blog.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author TIM
 * @create 2016-11-16 14
 **/
@Aspect
@Component
public class LogAspect {

    private Logger logger = LoggerFactory.getLogger(getClass());

    ThreadLocal<Long> startTime = new ThreadLocal<>();

    @Pointcut("execution(public * com.jt.blog.controller..*.*(..))")
    public void allController(){

    }

    @Before("allController()")
    public void tip(JoinPoint joinPoint){
        startTime.set(System.currentTimeMillis());
        Object[] args = joinPoint.getArgs();
        logger.info("REQUEST : " + Arrays.toString(args));
    }

    @AfterReturning(returning = "ret", pointcut = "allController()")
    public void doAfterReturning(Object ret) throws Throwable {
        // 处理完请求，返回内容
        logger.info("RESPONSE : " + ret);
        logger.info("SPEND TIME : " + (System.currentTimeMillis() - startTime.get()));
    }


}
